package controller.admin;


import Message.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import service.CategoryRelatedService;

@Slf4j
@RequestMapping("/admin/shop")
@RestController
public class ShopController {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;



    @PutMapping("/{status}")
    public Result setStatus(@PathVariable("status") Integer status) {
        try {
            // 测试Redis连接
            redisTemplate.getConnectionFactory().getConnection().ping();
            
            // 先删除旧数据，避免序列化冲突
            redisTemplate.delete("SHOP_STATUS");
            // 设置新数据
            redisTemplate.opsForValue().set("SHOP_STATUS", status);
            log.info("店铺运营状态是：{}", status==1?"营业中":"未营业");
            return Result.success();
        } catch (Exception e) {
            log.error("设置店铺状态失败：{}", e.getMessage());
            if (e.getMessage().contains("Unable to connect")) {
                return Result.error("Redis服务器连接失败，请检查Redis服务是否启动");
            }
            return Result.error("设置店铺状态失败：" + e.getMessage());
        }
    }
    @GetMapping("/status")
    public Result getStatus() {
        try {
            Object statusObj = redisTemplate.opsForValue().get("SHOP_STATUS");
            Integer status = null;
            
            if (statusObj != null) {
                // 使用JSON序列化器后，直接转换即可
                if (statusObj instanceof Integer) {
                    status = (Integer) statusObj;
                } else if (statusObj instanceof Number) {
                    status = ((Number) statusObj).intValue();
                } else {
                    status = Integer.valueOf(statusObj.toString());
                }
            }

            log.info("店铺运营状态是：{}", status==1?"营业中":"未营业");
            return Result.success(status);
        } catch (Exception e) {
            log.error("获取店铺状态失败：{}", e.getMessage());
            return Result.error("获取店铺状态失败，请检查Redis连接");
        }
    }






}
